#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{

  pid_t id=fork();
  if(id==0)
  {
    //child
    int count=0;
    while(count<10)
    {
      printf("I am child, pid:%d,ppid:%d\n",getpid(),getppid());
      sleep(2);
      count++;
    }
    exit(1);
  }

  while(1)
  {
  int status =0;
  pid_t ret=waitpid(id,&status,WNOHANG);//非阻塞等待,轮询检测方案
  if(ret>0)
  {
    printf("wait sueccess!\n");
    printf("exit code: %d\n",WEXITSTATUS(status));
    break;
  }
  else if(ret==0)//子进程没有退出，等待成功
  {
    printf("father do other things\n");
    sleep(1);
  }
  else{

  printf("wait error!\n");
  break;
  }
  }
  return 0;
}
